package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AlpContractDetail;
import com.ruoyi.system.service.IAlpContractDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AlpContractStage;
import com.ruoyi.system.service.IAlpContractStageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同阶段表Controller
 * 
 * @author zhangjun
 * @date 2020-02-13
 */
@Controller
@RequestMapping("/system/stage")
public class AlpContractStageController extends BaseController
{
    private String prefix = "system/stage";
    @Autowired
    private IAlpContractDetailService alpContractDetailService;
    @Autowired
    private IAlpContractStageService alpContractStageService;

    @RequiresPermissions("system:stage:view")
    @GetMapping()
    public String stage()
    {
        return prefix + "/stage";
    }

    /**
     * 选择合同树
     */
    @GetMapping("/selectContractTree")
    public String selectContractTree()
    {
        return prefix + "/tree";
    }

    /**
     * 新增合同阶段表,已确定合同对象
     */
    @GetMapping("/add/{contractId}")
    public String add(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        AlpContractDetail alpContractDetail = alpContractDetailService.selectAlpContractDetailById(contractId);
        mmap.put("alpContractDetail", alpContractDetail);
        return prefix + "/add";
    }

    /**
     * 查询合同阶段表列表
     */
    @RequiresPermissions("system:stage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AlpContractStage alpContractStage)
    {
        startPage();
        List<AlpContractStage> list = alpContractStageService.selectAlpContractStageList(alpContractStage);
        return getDataTable(list);
    }

    /**
     * 导出合同阶段表列表
     */
    @RequiresPermissions("system:stage:export")
    @Log(title = "合同阶段表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AlpContractStage alpContractStage)
    {

        List<AlpContractStage> list = alpContractStageService.selectAlpContractStageList(alpContractStage);
        for (AlpContractStage stage : list) {
            AlpContractDetail contractDetail = alpContractDetailService.selectAlpContractDetailById(stage.getContractId());
            stage.setContract(contractDetail);
        }
        ExcelUtil<AlpContractStage> util = new ExcelUtil<AlpContractStage>(AlpContractStage.class);
        return util.exportExcelYingShou(list, "应收表");
    }

    /**
     * 新增合同阶段表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 新增保存合同阶段表
     */
    @RequiresPermissions("system:stage:add")
    @Log(title = "合同阶段表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AlpContractStage alpContractStage)
    {
        long no = alpContractStageService.selectMaxStageNo(alpContractStage.getContractId());
        alpContractStage.setStageNo(no + 1);
        return toAjax(alpContractStageService.insertAlpContractStage(alpContractStage));
    }

    /**
     * 修改合同阶段表
     */
    @GetMapping("/edit/{stageId}")
    public String edit(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        AlpContractStage alpContractStage = alpContractStageService.selectAlpContractStageById(stageId);
        mmap.put("alpContractStage", alpContractStage);
        return prefix + "/edit";
    }

    /**
     * 修改保存合同阶段表
     */
    @RequiresPermissions("system:stage:edit")
    @Log(title = "合同阶段表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AlpContractStage alpContractStage)
    {
        return toAjax(alpContractStageService.updateAlpContractStage(alpContractStage));
    }

    /**
     * 删除合同阶段表
     */
    @RequiresPermissions("system:stage:remove")
    @Log(title = "合同阶段表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(alpContractStageService.deleteAlpContractStageByIds(ids));
    }

}
